/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication31;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.dnd.DragSource;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import java.awt.Cursor;

public class GraphPlot extends JFrame {

    private int draggedX = 55;
    private int draggedY = 55;
    private int draggedW = 22;
    private int draggedH = 22;
    LinePanel linePanel = new LinePanel();
    DragSource dragSource;
    Rectangle2D rectangle1, rectangle2;
    Boolean rectVisble1, rectVisble2;
    Rectangle2D draggedRectangle;
    Line2D line;
    int xstart = 0;
    int ystart = 0;
    int x1 = 55;
    int y1 = 55;
    int x2 = 11;
    int y2 = 11;
    int lineX1 = x1;
    int lineY1 = y1;
    int lineX2 = x2;
    int lineY2 = y2;

    GraphPlot() {
        rectVisble1 = true;
        rectVisble2 = true;
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        initContentPane();
        pack();
        setLocationRelativeTo(null);
        setSize(333, 333);
        setVisible(true);
        JPanel jp = new JPanel();
        JTree jt = new JTree();
        jt.setVisible(true);
    }

    private void initContentPane() {
        linePanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {

            public void mouseDragged(java.awt.event.MouseEvent evt) {
                MouseDragged(evt);
            }
        });
        linePanel.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mousePressed(java.awt.event.MouseEvent evt) {
                MousePressed(evt);
            }
        });
        linePanel.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {
                MouseReleased(evt);
            }
        });
        getContentPane().add(linePanel);
    }

    private void MouseDragged(java.awt.event.MouseEvent evt) {
        rectVisble1 = true;
        rectVisble2 = true;
        getContentPane().setCursor(
                Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
        if (draggedRectangle.equals(rectangle1)) {
            rectVisble1 = false;
            movedragged(evt.getX(), evt.getY());
            lineX1 = evt.getX();
            lineY1 = evt.getY();
        }
        if (draggedRectangle.equals(rectangle2)) {
            rectVisble2 = false;
            movedragged(evt.getX(), evt.getY());
            lineX2 = evt.getX();
            lineY2 = evt.getY();
        }

        this.repaint();
    }

    private void MousePressed(java.awt.event.MouseEvent evt) {
        xstart = evt.getX();
        ystart = evt.getY();

        draggedRectangle = new Rectangle2D.Double(xstart, ystart, 22, 22);
        if (!draggedRectangle.equals(null)
                && rectangle1.contains(evt.getX(), evt.getY())) {
            System.out.println("rectangle1");
            draggedRectangle = rectangle1;
        }
        if (!draggedRectangle.equals(null)
                && rectangle2.contains(evt.getX(), evt.getY())) {
            System.out.println("rectangle2");
            draggedRectangle = rectangle2;
        }
        repaint();
    }

    private void MouseReleased(java.awt.event.MouseEvent evt) {
        getContentPane().setCursor(
                Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        if (draggedRectangle.equals(rectangle1)) {
            x1 = evt.getX();
            y1 = evt.getY();
        }
        if (draggedRectangle.equals(rectangle2)) {
            x2 = evt.getX();
            y2 = evt.getY();
        }

        this.repaint();
    }

    private void movedragged(int x, int y) {
        int OFFSET = 1;
        if ((draggedX != x) || (draggedY != y)) {
            repaint(draggedX, draggedY, draggedW + OFFSET, draggedH + OFFSET);
            draggedX = x;
            draggedY = y;
            repaint(draggedX, draggedY, draggedW + OFFSET, draggedH + OFFSET);
        }
    }

    class LinePanel extends JPanel {

        LinePanel() {
            super();
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.RED);
            rectangle1 = new Rectangle2D.Double(x1, y1, 22, 22);
            rectangle2 = new Rectangle2D.Double(x2, y2, 22, 22);
            line = new Line2D.Double(lineX1, lineY1, lineX2, lineY2);
            // Исходные прямоугольники

            if (rectVisble1 == true) {
                g2.draw(rectangle1);
                g2.draw(line);
            }
            if (rectVisble2 == true) {
                g2.draw(rectangle2);
                g2.draw(line);
            }

            g2.drawRect(draggedX, draggedY, draggedW, draggedH);
            g2.drawLine(lineX1, lineY1, lineX2, lineY2);
            // Изображается во время движения
        }
    }

    public static void main(String[] args) {
        new GraphPlot();
    }
}